﻿using System;

namespace UnitDemo
{
    public class HexUtil
    {
        /// <summary>
        /// 示例 ： 30 -> 0x30
        /// 仅适用于1字节的BCD编码
        /// </summary>
        /// <param name="num"></param>
        /// <returns></returns>
        public static byte int2Byte(int num)
        {
            // 30 -> "30" (“0x30”) -> 48 (10进制) == 0x30 
            string str = num.ToString();
            int hexInt = Convert.ToInt32(str, 16);
            byte b = (byte)hexInt;
            return b;
        }

        /// <summary>
        /// 示例 ：0x30 -> 30
        /// 仅适用于1字节的BCD编码
        /// </summary>
        /// <param name="num"></param>
        /// <returns></returns>
        public static int byte2Int(byte num)
        {
            //0x30 -> "0x30"
            string hexStr = num.ToString("X2");
            hexStr = hexStr.Remove(0, 2);
            int result = Convert.ToInt32(hexStr);
            return result;
        }
        
    }
}